﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


namespace QuanLyQuangCao
{
    public partial class frmNhanVien_View : Form
    {
        #region Khai bao bien
        string strconn = @"Data Source=thonglh;Initial Catalog=QuanLyQuangCao;Integrated Security=True";
        public Myconection myconn = new Myconection();
        
        
        public SqlConnection sc = new SqlConnection();
        public SqlDataAdapter da = new SqlDataAdapter();
        public SqlCommand scmd = new SqlCommand();
        public DataTable dt = new DataTable();
        
        public DataSet ds = new DataSet();
        public BindingSource bds = new BindingSource();
        public static frmNhanVien_View view;

        public frmNHANVIEN_Edit frmEdit = new frmNHANVIEN_Edit();
       
        #endregion

        #region Contructor
        public frmNhanVien_View()
        {
            InitializeComponent();

            view = this;

            this.btThem.Click += new EventHandler(this.btThem_Click);
            this.btSua.Click += new EventHandler(btSua_Click);
            this.btXoa.Click += new EventHandler(btXoa_Click);
            this.btThoat.Click += new EventHandler(btThoat_Click);
            this.Load += new EventHandler(frmNhanVien_View_Load);
        }

        #endregion

        #region Method

        public void Update(BindingSource bds)
        {
            dgvNhanvien.DataSource = bds;
        }

        //Xac dinh load form New hay Edit
        public enum myEdit
        {
            New = 1,
            Edit = 2,
        }

        public void Edit(myEdit myNew_Edit)
        {
            if (myNew_Edit == myEdit.New)
            {
                frmEdit.Text = "Thêm mới nhân viên";
                frmEdit.Load(frmNHANVIEN_Edit.myEdit.New);
            }

            if (myNew_Edit == myEdit.Edit)
            {
                frmEdit.Load(frmNHANVIEN_Edit.myEdit.Edit);
                frmEdit.Text = "Sửa nhân viên";
            }

        }

        #endregion

        #region FillData
        
        public void FillData()
        {
            
            //----------------
            string strKHACHANG = "SELECT * FROM NHANVIEN";
            frmNhanVien_View.view.myconn.GetData(strKHACHANG, strconn, ds, "NhanVien");
            da = new SqlDataAdapter(strKHACHANG, strconn);
            dt = new DataTable();
            da.Fill(dt);
            bds.DataSource = dt;
            da.Dispose();
            dgvNhanvien.DataSource = null;
            dgvNhanvien.DataSource = bds;
            //----------
            //string strKHACHANG = "SELECT * FROM KHACHANG";
            //da = new SqlDataAdapter(strKHACHANG, strconn);
            //dt = new DataTable();
            //da.Fill(dt);
            //bds.DataSource = dt;
            //da.Dispose();
            //dgvDmKh.DataSource = null;
            //dgvDmKh.DataSource = bds;
            //dgvDmKh.Refresh();
            //------------
            //Su dung Dataset
            //da.Fill(ds, "KHACHANG");
            //dt = ds.Tables["KHACHANG"];
            //da.Dispose();
            //dgvDmKh.DataSource = ds.Tables[0];
        }
        public void frmNhanVien_View_Load(object sender, EventArgs e)
        {
            FillData();
        }

        #endregion

        #region Event
        public void btThem_Click(object sender, EventArgs e)
        {
            Edit(myEdit.New);
           // frmEdit.txtChiet_Khau.Text = "0";
            frmEdit.Load(frmNHANVIEN_Edit.myEdit.New);
            frmEdit.ShowDialog();
            
        }

        public void btSua_Click(object sender, EventArgs e)
        {
            Edit(myEdit.Edit);
            if (dgvNhanvien.Rows.Count > 0)
            {
                int row = Convert.ToInt32(dgvNhanvien.CurrentRow.Index);
                frmEdit.Load(frmNHANVIEN_Edit.myEdit.Edit);
                frmEdit.txtMa_Nhan_Vien.Text = dgvNhanvien.Rows[row].Cells[0].Value.ToString();
                frmEdit.txtTen_Nhan_Vien.Text = dgvNhanvien.Rows[row].Cells[1].Value.ToString();
                frmEdit.txtMat_Khau.Text = dgvNhanvien.Rows[row].Cells[2].Value.ToString();
                frmEdit.txtTrang_Thai.Text = dgvNhanvien.Rows[row].Cells[3].Value.ToString();
                frmEdit.txtKieu.Text = dgvNhanvien.Rows[row].Cells[3].Value.ToString();
                frmEdit.setMaNhanVien(dgvNhanvien.Rows[row].Cells[0].Value.ToString());
                frmEdit.ShowDialog();
            }
            else
                MessageBox.Show("Chưa có dữ liệu", "Thông báo", MessageBoxButtons.OK);
        }

        public void btXoa_Click(object sender, EventArgs e)
        {
            int row = Convert.ToInt32(dgvNhanvien.CurrentRow.Index);
            string strMa_Nhan_Vien = dgvNhanvien.Rows[row].Cells[0].Value.ToString();
            if (MessageBox.Show("Bạn có thực sự muốn xóa dòng đã chọn không?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                if (dgvNhanvien.CurrentCell != null)
                    dgvNhanvien.Rows.RemoveAt(dgvNhanvien.SelectedCells[0].RowIndex);
                sc = new SqlConnection(strconn);
                string str = "DELETE FROM KHACHANG WHERE Ma_Nhan_Vien = '" + strMa_Nhan_Vien + "'";
                scmd = new SqlCommand(str, sc);
                sc.Open();
                scmd.ExecuteNonQuery();
                sc.Close();
            }
            else
                return;
        }
        //public void btXoa_Click(object sender, EventArgs e)
        //{
        //    int row = Convert.ToInt32(dgvDmKh.CurrentRow.Index);
        //    string strMa_Nhan_Vien = dgvDmKh.Rows[row].Cells[0].Value.ToString();
        //    sc = new SqlConnection(strconn);
        //    scmd = new SqlCommand("Sp_Delete_KHACHANG", sc);
        //    scmd.CommandType = CommandType.StoredProcedure;
        //    SqlParameter sptKHACHANG = new SqlParameter("@Ma_Nhan_Vien", SqlDbType.NVarChar);
        //    //scmd.Parameters.Add("@Ma_Nhan_Vien", SqlDbType.NVarChar).Value = strMa_Nhan_Vien;
        //    //sptKHACHANG.Value = strMa_Nhan_Vien;
        //    sc.Open();
        //    scmd.ExecuteNonQuery();
        //    sc.Close();
        //}

        public void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }
        #endregion
    }
}
